﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Workflow.ComponentModel;
using System.ComponentModel;
using Microsoft.Office.Word.Server.Conversions;
using System.Workflow.ComponentModel.Compiler;
using Microsoft.SharePoint.WorkflowActions;

namespace SP2010WordAutomation.Workflow
{
    public class GetConversionJobStatusActivity : Activity
    {
        public static DependencyProperty ConversionJobIdProperty = DependencyProperty.Register("ConversionJobId", typeof(string), typeof(GetConversionJobStatusActivity));
        public static DependencyProperty ConversionJobStatusProperty = DependencyProperty.Register("ConversionJobStatus", typeof(string), typeof(GetConversionJobStatusActivity));
        public static DependencyProperty __ContextProperty = DependencyProperty.Register("__Context", typeof(WorkflowContext), typeof(GetConversionJobStatusActivity));
                
        [ValidationOption(ValidationOption.Required)]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string ConversionJobId
        {
            get { return (string)base.GetValue(ConversionJobIdProperty); }
            set { base.SetValue(ConversionJobIdProperty, value); }
        }

        [ValidationOption(ValidationOption.Required)]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string ConversionJobStatus
        {
            get { return (string)base.GetValue(ConversionJobStatusProperty); }
            set { base.SetValue(ConversionJobStatusProperty, value); }
        }

        [ValidationOption(ValidationOption.Required)]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public WorkflowContext __Context
        {
            get { return ((WorkflowContext)(base.GetValue(__ContextProperty))); }
            set { base.SetValue(__ContextProperty, value); }
        }

        protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
        {
            Microsoft.Office.Word.Server.Service.WordServiceApplicationProxy wordServiceApplicationProxy =
                    Common.GetWordServiceApplicationProxy(__Context.Site.WebApplication);
            ConversionJobStatus status = new ConversionJobStatus(wordServiceApplicationProxy, new System.Guid(ConversionJobId), null);
            
            if (status.Count == status.Succeeded)
            {
                ConversionJobStatus = "Completed";
            }
            else if (status.Count == status.Failed)
            {
                ConversionJobStatus = "Failed";
            }
            else if (status.NotStarted > 0 || status.InProgress > 0)
            {
                ConversionJobStatus = "Running";
            }

            return ActivityExecutionStatus.Closed;
        }

        
    }
}
